/******************************************************************************
Project    : Real time rtsp server program
*******************************************************************************
File Name    :  <RtspServer.cpp>:  real time rtsp server interface head file
Module Name  :  
Description  :  None
History      :
 No.   Ver.     Date        Designed by  Comment
-----+--------+----------+------------+---------------------------------------
 001   0.00.01  05/25/2015  <3G@LIURENHUA>   <real time rtsp server interface head file>
*******************************************************************************/
#ifndef _RTSP_SERVER_INTERFACE_H
#define _RTSP_SERVER_INTERFACE_H
#include <pthread.h>

#include "queue.h"
#include "RealTimeRtspServer.h"
#include "AudioRTimeServerMediaSubsession.h"
#include "VideoRTimeServerMediaSubsession.h"
#include "Mp2TsRTimeServerMediaSubsession.h"

#define AUDIO_ENABLE
/******************************************************************************
  macro define
 ******************************************************************************/
 #define DEFAULT_PORT				(8899)
 #define MAX_QUEUE_SIZE			(8)
 #define DELAY_SIZE						(1)

/* Interface usage as follows:
 *  1  start rtsp server
 *  1) call RtspServerInterface() constructor function to create rtsp server object 
 *  2) call RtspServerInterface::vQueueInit create queue for rtsp server object
 *  3) call vCreateSMS() create server media session object
 *  4) call vAddVideoSMSSToSMS, vAddAudioSMSSToSMS or vAddTsSMSSToSMS add video,
 *       audio or ts server media subsession to SMS
 *  5) call vStartTaskScheduler() start rtsp server
 * 
 *  2  switch rtsp server (change sms, e.g change h265 to h264)
 *  1) call vResetRtspServer() function (delete old sms)
 *  2) call vStopTaskScheduler() function stop event loop
 *  3) call RtspServerInterface::vQueueDInit() function delete old queue 
 *       relation to rtsp server object
 *  4) call RtspServerInterface::vQueueInit create new queue for rtsp server object
 *  5) call vCreateSMS() create new server media session object
 *  6) call vAddVideoSMSSToSMS, vAddAudioSMSSToSMS or vAddTsSMSSToSMS add video,
 *       audio or ts new server media subsession to SMS
 *  7) call vStartTaskScheduler() start rtsp server
 * 
 *  3 stop rtsp server
 *  1) call ~RtspServerInterface()
 *  2) call RtspServerInterface::vQueueDInit() function delete queue relation to rtsp server 
 *       object
 * */
/******************************************************************************
  class declaration
 ******************************************************************************/
class RtspServerInterface
{
public:
	static void vQueueInit(queue_t** pstQue, QueType eType = REALTIME_FIRST, 
		int msize = MAX_QUEUE_SIZE, int delay = DELAY_SIZE);

#ifdef DEBUG_PACKETNO
	static void vQuePushData(int number,
		const char *encType, const uint8_t *mBuffer, size_t mSize, queue_t* pstQue);
#else
	static void vQuePushData(
		const char *encType, const uint8_t *mBuffer, size_t mSize, queue_t* pstQue);
#endif /* DEBUG_PACKETNO */
		
	static void vQueueDInit(queue_t** pstQue);
	
	RtspServerInterface(u_int16_t usPort = 0);
	~RtspServerInterface();
	
	/* Create sms */
	void vCreateSMS();
	
	/* Add amr or adts smss to sms */
	void vAddAudioSMSSToSMS(tEsFmt eCodecId, CH_CFG_et eChCfg, FS_IDX_et eFsIdx, 
		PROFILE_et eProfile, Boolean bWideBand, u_int8_t ucChNum, queue_t* pstQue);

	/* Add h264 or h265 smss to sms */
	void vAddVideoSMSSToSMS(
		tEsFmt eCodecId, u_int8_t ucFrmRate, queue_t* pstQue);
	
	/* Add ts smss to sms */
	void vAddTsSMSSToSMS(queue_t* pstQue);
	
	/* Reset rtsp server  */
	void vResetRtspServer();
	
	/* Start task sheduler */
	void vStartTaskScheduler();
	
	/* Stop task sheduler */
	void vStopTaskScheduler();

	/* Get start flag */
	bool bGetStartFlag(){return start;}
	
private:
	/* Usage environment pointer  */
	UsageEnvironment *pctUsgEnv;
	/* Real time rtsp server pointer */
	RealTimeRtspServer *pctSvr;
	/* The thread indentity */
	pthread_t threadId;
	/* The start flag */
	bool start;
};
#endif /* _RTSP_SERVER_INTERFACE_H */
